// 购物车数据
let cart = JSON.parse(localStorage.getItem('cart')) || [];

// 更新购物车计数
function updateCartCount() {
    const cartCount = document.getElementById('cart-count');
    if (cartCount) {
        cartCount.textContent = cart.reduce((sum, item) => sum + item.quantity, 0);
    }
}

// 更新购物车总价
function updateCartTotal() {
    const total = cart.reduce((sum, item) => {
        const price = parseFloat(item.price.replace('¥', ''));
        return sum + price * item.quantity;
    }, 0);
    
    const totalElement = document.getElementById('cart-total');
    if (totalElement) {
        totalElement.textContent = `¥${total.toFixed(2)}`;
    }
}

// 渲染购物车项目
function renderCartItems() {
    const container = document.getElementById('cart-items');
    if (!container) return;

    if (cart.length === 0) {
        container.innerHTML = '<p class="text-gray-500 text-center py-8">购物车是空的</p>';
        return;
    }

    container.innerHTML = cart.map(item => `
        <div class="flex items-center justify-between border-b border-gray-200 py-4">
            <div class="flex items-center">
                <img src="${item.image}" alt="${item.name}" class="w-20 h-20 object-cover rounded">
                <div class="ml-4">
                    <h3 class="text-lg font-medium">${item.name}</h3>
                    <p class="text-[#B3927F]">${item.price}</p>
                </div>
            </div>
            <div class="flex items-center">
                <button 
                    onclick="updateQuantity(${item.id}, ${item.quantity - 1})"
                    class="px-3 py-1 border border-gray-300 rounded-l"
                >-</button>
                <span class="px-4 py-1 border-t border-b border-gray-300">${item.quantity}</span>
                <button 
                    onclick="updateQuantity(${item.id}, ${item.quantity + 1})"
                    class="px-3 py-1 border border-gray-300 rounded-r"
                >+</button>
                <button 
                    onclick="removeFromCart(${item.id})"
                    class="ml-4 text-red-500 hover:text-red-700"
                >
                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                    </svg>
                </button>
            </div>
        </div>
    `).join('');
}

// 添加到购物车
function addToCart(productId) {
    const product = products.find(p => p.id === productId);
    if (!product) return;

    const existingItem = cart.find(item => item.id === productId);
    if (existingItem) {
        existingItem.quantity += 1;
    } else {
        cart.push({
            id: product.id,
            name: product.name,
            price: product.price,
            image: product.image,
            quantity: 1
        });
    }

    localStorage.setItem('cart', JSON.stringify(cart));
    updateCartCount();
    updateCartTotal();
    renderCartItems();

    // 添加动画效果
    const cartIcon = document.querySelector('.cart-float');
    cartIcon.classList.add('cart-bounce');
    
    setTimeout(() => {
        cartIcon.classList.remove('cart-bounce');
    }, 1000);
}

// 更新商品数量
function updateQuantity(productId, newQuantity) {
    if (newQuantity < 1) {
        removeFromCart(productId);
        return;
    }

    const item = cart.find(item => item.id === productId);
    if (item) {
        item.quantity = newQuantity;
        localStorage.setItem('cart', JSON.stringify(cart));
        updateCartCount();
        updateCartTotal();
        renderCartItems();
    }
}

// 从购物车移除商品
function removeFromCart(productId) {
    cart = cart.filter(item => item.id !== productId);
    localStorage.setItem('cart', JSON.stringify(cart));
    updateCartCount();
    updateCartTotal();
    renderCartItems();
}

// 结算
function checkout() {
    alert('感谢您的购买！');
    cart = [];
    localStorage.removeItem('cart');
    updateCartCount();
    updateCartTotal();
    renderCartItems();
}

// 初始化
document.addEventListener('DOMContentLoaded', () => {
    updateCartCount();
    updateCartTotal();
    renderCartItems();
}); 